package com.isunland.managesystem.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseButterKnifeAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.CompanyForumTopic;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.widget.CustomTextView;
import com.isunland.managesystem.widget.RoundImageView;
import java.util.ArrayList;
import java.util.Date;

/**
 * 话题信息类
 *
 * @author wu
 */
public class AgriculturalForumTopicListAdapter
    extends BaseButterKnifeAdapter<CompanyForumTopic.actualObject> {

  private final ImageLoader mImageLoader;
  private boolean isEmpty;

  // Constructors
  public AgriculturalForumTopicListAdapter(BaseVolleyActivity context,
      ArrayList<CompanyForumTopic.actualObject> objects) {
    super(context, objects);
    mImageLoader = RequestManager.getImageLoader();
    this.isEmpty = super.isEmpty();
  }

  public AgriculturalForumTopicListAdapter(BaseVolleyActivity context,
      ArrayList<CompanyForumTopic.actualObject> objects, boolean isEmpty) {
    super(context, objects);
    mImageLoader = RequestManager.getImageLoader();
    this.isEmpty = isEmpty;
  }

  @Override protected int setLayoutId() {
    return R.layout.adapter_forum_topic_item;
  }

  @NonNull @Override protected BaseViewHolder initHolder(View convertView) {
    return new ViewHolder(convertView);
  }

  @Override protected void initView(CompanyForumTopic.actualObject item, BaseViewHolder baseHolder,
      int position) {
    ViewHolder vh = (ViewHolder) baseHolder;
    vh.tvTitleForum.setText(item.getTopicTitle());
    vh.tvContentForum.setText(item.getTopicContents());
    vh.tvNameForum.setText(item.getRegStaffName());

    //if (position == 1) item.setColor("#FF0000");
    //if (position == 3) item.setColor("#FCAD5A");

    String color = item.getLabel();

    //vh.llBg.setBackgroundColor(Color.parseColor(MyStringUtil.isEmpty(color) ? "#FFFFFF" : color));
    //显示或隐藏标签
    if ((!MyStringUtil.isEmpty(color))
        && (!MyStringUtil.isEmpty(item.getLabelName()))
        && (MyStringUtil.contains(color, "#"))) {

      vh.ivTag.setText(item.getLabelName());
      vh.ivTag.setSolidColor(Color.parseColor(color));
      vh.ivTag.setVisibility(View.VISIBLE);
    } else {
      vh.ivTag.setVisibility(View.GONE);
    }
    //显示或隐藏手机号.
    if (!MyStringUtil.isEmpty(item.getMobile())) {
      vh.tvPhone.setVisibility(View.VISIBLE);
      vh.tvPhone.setText(item.getMobile());
    } else {
      vh.tvPhone.setVisibility(View.GONE);
    }
    // 头像
    String filePath = item.getPicture();
    if (!TextUtils.isEmpty(filePath)) {
      filePath = FileUtil.getThumbNailFilePath(filePath);
      String urlImage = ApiConst.getDownLoadUrlByGet(filePath);

      ImageListener listener = ImageLoader.getImageListener(vh.ivImageForum, // 控件名
          R.drawable.photo, R.drawable.photo); // 默认头像
      mImageLoader.get(urlImage, listener);
    } else {
      vh.ivImageForum.setImageResource(R.drawable.photo);
    }

    /*图片改为显示缩略图*/

    //String pictureA = item.getTopicPicA();
    //String pictureB = item.getTopicPicB();
    //String pictureC = item.getTopicPicC();
    //String pictureD = item.getTopicPicD();

    String pictureA = item.getThumbnailA();
    String pictureB = item.getThumbnailB();
    String pictureC = item.getThumbnailC();
    String pictureD = item.getThumbnailD();
    String pictureE = item.getThumbnailE();
    String pictureF = item.getThumbnailF();

    if ((TextUtils.isEmpty(pictureA)
        && TextUtils.isEmpty(pictureB)
        && TextUtils.isEmpty(pictureC)
        && TextUtils.isEmpty(pictureD)
        && TextUtils.isEmpty(pictureE)
        && TextUtils.isEmpty(pictureF))) {
      vh.ivPictureAFourm.setVisibility(View.GONE);
      vh.ivPictureBFourm.setVisibility(View.GONE);
      vh.ivPictureCFourm.setVisibility(View.GONE);
      vh.ivPictureDFourm.setVisibility(View.GONE);
      vh.ivPictureEFourm.setVisibility(View.GONE);
      vh.ivPictureFFourm.setVisibility(View.GONE);
    } else {

      volleyPostGetPic(pictureA, vh.ivPictureAFourm);
      volleyPostGetPic(pictureB, vh.ivPictureBFourm);
      volleyPostGetPic(pictureC, vh.ivPictureCFourm);
      volleyPostGetPic(pictureD, vh.ivPictureDFourm);
      volleyPostGetPic(pictureE, vh.ivPictureEFourm);
      volleyPostGetPic(pictureF, vh.ivPictureFFourm);
    }

    // 附件
    String forumFilePath = item.getTopicFile();
    if (!TextUtils.isEmpty(forumFilePath)) {
      vh.tvFileForum.setVisibility(View.VISIBLE);
      forumFilePath =
          forumFilePath.substring(forumFilePath.lastIndexOf("/") + 1, forumFilePath.length());
      vh.tvFileForum.setText(forumFilePath);
    } else {
      vh.tvFileForum.setVisibility(View.GONE);
    }

    // 服务器返回的时间
    String times = item.getRegDate();
    String publishTime = DateUtil.dateToTimeInMillis(context, times, new Date());
    vh.tvTimeForum.setText(publishTime);
    vh.tvAddressForum.setText(item.getLocation());//发帖地址
    vh.tvAddressForum.setVisibility(
        TextUtils.isEmpty(item.getLocation()) ? View.GONE : View.VISIBLE);
    vh.tvNumberForum.setText(item.getDiscussCount());
  }

  private void volleyPostGetPic(String pictureUrl, ImageView imageView) {
    if (!TextUtils.isEmpty(pictureUrl)) {
      imageView.setVisibility(View.VISIBLE);
      //            pictureUrl = FileUtil.getThumbNailFilePath(pictureUrl);//手动拼缩略图路径
      String urlImage = ApiConst.getDownLoadUrlByGet(pictureUrl);
      ImageListener listener = ImageLoader.getImageListener(imageView, // 控件名
          R.drawable.fail_pic, R.drawable.fail_pic); // 默认图片
      mImageLoader.get(urlImage, listener);
    } else {
      imageView.setVisibility(View.GONE);
    }
  }

  protected class ViewHolder extends BaseViewHolder {
    @BindView(R.id.tv_title_forum) TextView tvTitleForum;
    @BindView(R.id.tv_content_forum) TextView tvContentForum;
    @BindView(R.id.iv_pictureA_fourm) ImageView ivPictureAFourm;
    @BindView(R.id.iv_pictureB_fourm) ImageView ivPictureBFourm;
    @BindView(R.id.iv_pictureC_fourm) ImageView ivPictureCFourm;
    @BindView(R.id.iv_pictureD_fourm) ImageView ivPictureDFourm;
    @BindView(R.id.iv_pictureE_fourm) ImageView ivPictureEFourm;
    @BindView(R.id.iv_pictureF_fourm) ImageView ivPictureFFourm;
    @BindView(R.id.tv_file_forum) TextView tvFileForum;
    @BindView(R.id.iv_image_forum) RoundImageView ivImageForum;
    @BindView(R.id.tv_name_forum) TextView tvNameForum;
    @BindView(R.id.tv_phone) TextView tvPhone;
    @BindView(R.id.tv_address_forum) TextView tvAddressForum;
    @BindView(R.id.tv_time_forum) TextView tvTimeForum;
    @BindView(R.id.tv_number_forum) TextView tvNumberForum;
    @SuppressWarnings("unused") @BindView(R.id.ll_bg) LinearLayout llBg;
    @BindView(R.id.iv_tag) CustomTextView ivTag;

    ViewHolder(View view) {
      super(view);
      //农业论坛显示手机号.
      tvPhone.setVisibility(View.VISIBLE);
    }
  }

  @Override public boolean isEmpty() {
    return isEmpty;
  }
}
